package web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bouncycastle.util.encoders.Base64;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import dao.UserDAO;
import validator.*;
import web.LoggedUser;
import beans.*;

public class HomeUserController extends MultiActionController {
	private UserDAO userDAO;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	private LoggedUser loggedUser;

	public void setLoggedUser(LoggedUser loggedUser) {
		this.loggedUser = loggedUser;
	}

	private ChangePasswordValidator changePasswordValidator;
	private ChangeUserDataValidator changeUserDataValidator;

	public void setChangePasswordValidator(
			ChangePasswordValidator changePasswordValidator) {
		this.changePasswordValidator = changePasswordValidator;
	}

	public void setChangeUserDataValidator(
			ChangeUserDataValidator changeUserDataValidator) {
		this.changeUserDataValidator = changeUserDataValidator;
	}

	public ModelAndView myaccount(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		User user = loggedUser.getLoggedUser();
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("user", user);
		return new ModelAndView("myaccount", modelMap);
	}

	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("logout");
	}

	public ModelAndView changepassword(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User user = loggedUser.getLoggedUser();
		String oldPassword = request.getParameter("oldPassword");
		String newPassword = request.getParameter("newPassword");
		String confirmPassword = request.getParameter("confirmPassword");
		oldPassword = userDAO.encodePassword(oldPassword);
		return changePasswordValidator.validate(user, oldPassword, newPassword,
				confirmPassword);
	}

public ModelAndView changeuserdata(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User user = loggedUser.getLoggedUser();
		String name = request.getParameter("name");
		String lastname = request.getParameter("lastname");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String city = request.getParameter("city");
		String country = request.getParameter("country");
		
		Date joined = user.getJoined();
		user.setName(name);
		user.setLastname(lastname);
		user.setEmail(email);
		user.setPhone(phone);
		user.setCity(city);
		user.setCountry(country);
		user.setJoined(joined);
		return changeUserDataValidator.validate(user);
	}



}
